<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: SendMailPortlet.php 412 2011-01-04 21:21:57Z steen.rabol $
 * @package Pradoportal.Porttlets.SendMailPortlet
 *
 */

Prado::using('Application.Portlets.Portlet');
//Prado::using('Application.Common.XMimeMail');
Prado::using('Application.Portlets.SendMailPortlet.Common.SendMailPortletRecord');
Prado::using('Application.Common.Data.PortalUserRecord');

class SendMailPortlet extends Portlet
{
	public function  getConfigName()
	{
		return get_class();
	}

	public function dataBind()
	{
		$this->hfMailSend->Value = 0;

		// Find Recipients
		$recipients = SendMailPortletRecord::finder()->findAllByactive(1);

		if(!$recipients || count($recipients) == 0)
		{
			// We use the site admin...
			$uid = PortalUserRecord::finder()->findByemail($this->Application->Parameters['AdminEmail']);

			if(!$uid)
			{
				$this->reportError(0,'user by admin e-mail {0} not found', array($this->Application->Parameters['AdminEmail']));
			}
			$this->RecipientId->Value		= $uid->id;
			$this->SingleRecipient->Visible	= true;
			$this->SingleRecipient->Text	= $uid->full_name;

			$this->valRec->Visible	= false;
			$this->valRec->Enabled	= false;
		}
		else if($recipients && count($recipients) == 1)
		{
			$row = $recipients[0];
			try
			{
				$this->RecipientId->Value		= $row->id;
				$this->SingleRecipient->Visible	= true;
				$this->SingleRecipient->Text	= $row->name;
				$this->valRec->Visible			= false;
				$this->valRec->Enabled			= false;
			}
			catch(Exception $e)
			{
				$this->Page->reportError(1,$e->getMessage());
			}
		}
		else
		{
			$this->RecipientId->Value				= -1;
			$this->MultiRecipient->Visible			= true;
			$this->MultiRecipient->DataSource	 	= $recipients;
			$this->MultiRecipient->DataTextField	= "name";
			$this->MultiRecipient->DataValueField	= "id";
			$this->MultiRecipient->dataBind();
		}
	}

	public function OnLoad($parm)
	{
		parent::OnLoad($parm);

		if(!$this->Page->IsPostBack)
		{
			$prop = @unserialize($this->ConfigRecord->value);
			if(is_array($prop))
			{
				if(isset($prop['legendtext']))
				{
					$this->createmail->GroupingText=$prop['legendtext'];
				}

				if(isset($prop['showlegend']))
				{
					if(TPropertyValue::ensureInteger($prop['showlegend']) == 0)
					{
						$this->createmail->GroupingText = "";
					}
				}
			}

			$mailsend	= TPropertyValue::ensureInteger($this->Request['mailsend']);

			if($mailsend === 0)
			{
				$this->Page->getClientScript()->registerFocusControl(($this->User->Id ? $this->Subject : $this->YourName));
				$this->createmail->Visible = true;
				$this->createmail->Enabled = true;
				$this->dataBind();
			}
			else
			{
				$this->mailsend->Visible = true;
				$this->mailsend->Enabled = true;
				$this->createmail->Visible = false;
				$this->createmail->Enabled = false;
			}
		}
		else
		{
			$this->mailsend->Visible = true;
			$this->mailsend->Enabled = true;
		}
	}

	public function sendmail($sender, $parm)
	{
		Prado::using('Application.Common.XMimeMail');
		if($this->Page->isValid)
		{
			if($this->RecipientId->Value != -1)
			{
				// Single recipient
				$id = $this->RecipientId->Value;
			}
			else
			{
				$id = $this->MultiRecipient->SelectedValue;
			}

			$email = $this->Page->DataAccess->createCommand("select * from tblsendemailportlet where id=$id")->queryRow();

			$xMail = new XMimeMail();
			$xMail->setCharset('iso-8859-1');
 			$xMail->setTextBody($this->YourMessage->Text);
 			$params['html_encoding'] = 'quoted-printable';
 			$params['text_encoding'] = 'quoted-printable';
 			$params['text_wrap'] = 998;
 			$xMail->prepare($params);

 			try
 			{
 				$xMail->send($email['name'], $email['email'], $this->YourName->Text, $this->YourEmail->Text, $this->Subject->Text);
 				unset($xMail);
 			}
 			catch(Exception $e)
 			{
 			}

			$this->Page->gotoPage($this->Page->RequestedPagePath, array('mailsend' =>1));
		}
	}
}
?>